package com.runen.infinitemovement.adapter;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.module.LoadMoreModule;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.runen.infinitemovement.R;
import com.runen.infinitemovement.model.entity.RankDtoListEntity;
import com.runen.infinitemovement.tool.CommonUtil;
import com.runen.infinitemovement.tool.GlideUtils;
import com.runen.infinitemovement.widget.imageview.RoundedCornersImage;
import com.runen.rxnetlibrary.utils.LogUtil;

public class FireRankingAdapter extends BaseQuickAdapter<RankDtoListEntity, BaseViewHolder> implements LoadMoreModule {
    private Context context;
    private int flag;
    private int totalNum;

    public void setTotalNum(int totalNum) {
        this.totalNum = totalNum;
    }

    public FireRankingAdapter(Context context) {
        super(R.layout.item_fire_ranking);
        this.context = context;
    }

    public FireRankingAdapter(Context context, int flag) {
        super(R.layout.item_fire_ranking);
        this.context = context;
        this.flag = flag;
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, RankDtoListEntity item) {
        LogUtil.e("totalNum", totalNum + "");
        LinearLayout ll_bg1 = helper.getView(R.id.ll_bg1);
        LinearLayout ll_bg = helper.getView(R.id.ll_bg);
        ImageView iv_ranking = helper.getView(R.id.iv_ranking);
        TextView tv_user_name = helper.getView(R.id.tv_user_name);
        TextView tv_ranking = helper.getView(R.id.tv_ranking);
        TextView tv_fire_num = helper.getView(R.id.tv_fire_num);
        TextView tv_my_team = helper.getView(R.id.tv_my_team);
        TextView tv_fire_num_progress = helper.getView(R.id.tv_fire_num_progress);
        RoundedCornersImage rci_user_header = helper.getView(R.id.rci_user_header);
        GlideUtils.loadImage(context, item.logo + "?x-oss-process=image/resize,m_fill,w_144,h_144", rci_user_header);
        tv_user_name.setText(item.name);
        tv_fire_num.setText(String.valueOf(item.tinderCount));
        int position = helper.getAdapterPosition();
        String ss = "贡献进度：%s";
        if (totalNum > 0) {
            double sum = (double) item.tinderCount / (double) totalNum * 100;
            tv_fire_num_progress.setText(String.format(ss, CommonUtil.Double2position(sum) + "%"));
        } else {
            tv_fire_num_progress.setText("贡献进度：0%");
        }

        if (flag != 0) {
            if (position % 2 == 0) {
                ll_bg1.setBackgroundColor(CommonUtil.getColor(R.color.color_FFF9F8F9));
            } else {
                ll_bg1.setBackgroundColor(CommonUtil.getColor(R.color.white));
            }
        }
        if (flag == 0) {
            switch (position) {
                case 0:
                    tv_my_team.setVisibility(View.VISIBLE);
                    switch (item.rank) {
                        case 1:
                            iv_ranking.setVisibility(View.VISIBLE);
                            tv_ranking.setText("0" + (item.rank));
                            tv_ranking.setTextSize(16);
                            ll_bg.setBackground(CommonUtil.getDrawable(R.drawable.bg_fffff6f6_dp7));
                            tv_ranking.setTextColor(CommonUtil.getColor(R.color.white));
                            GlideUtils.loadImage(context, CommonUtil.getDrawable(R.mipmap.ico_ranking1), iv_ranking);
                            break;
                        case 2:
                            iv_ranking.setVisibility(View.VISIBLE);
                            tv_ranking.setText("0" + (item.rank));
                            tv_ranking.setTextSize(16);
                            tv_ranking.setTextColor(CommonUtil.getColor(R.color.white));
                            ll_bg.setBackground(CommonUtil.getDrawable(R.drawable.bg_fffff6f6_dp7));
                            GlideUtils.loadImage(context, CommonUtil.getDrawable(R.mipmap.ico_ranking2), iv_ranking);
                            break;
                        case 3:
                            iv_ranking.setVisibility(View.VISIBLE);
                            tv_ranking.setText("0" + (item.rank));
                            tv_ranking.setTextSize(16);
                            ll_bg.setBackground(CommonUtil.getDrawable(R.drawable.bg_fffff6f6_dp7));
                            tv_ranking.setTextColor(CommonUtil.getColor(R.color.white));
                            GlideUtils.loadImage(context, CommonUtil.getDrawable(R.mipmap.ico_ranking3), iv_ranking);
                            break;
                        default:
                            ll_bg.setBackground(CommonUtil.getDrawable(R.drawable.bg_fffff6f6_dp7));
                            tv_ranking.setTextColor(CommonUtil.getColor(R.color.color_333333));
                            tv_ranking.setText("未上榜");
                            tv_ranking.setTextSize(13);
                            iv_ranking.setVisibility(View.GONE);
                            break;
                    }

                    break;
                case 1:
                    tv_my_team.setVisibility(View.GONE);
                    iv_ranking.setVisibility(View.VISIBLE);
                    tv_ranking.setText("0" + (position));
                    tv_ranking.setTextColor(CommonUtil.getColor(R.color.white));
                    tv_fire_num.setTextColor(CommonUtil.getColor(R.color.color_FFED3F37));
                    GlideUtils.loadImage(context, CommonUtil.getDrawable(R.mipmap.ico_ranking1), iv_ranking);
                    break;
                case 2:
                    tv_my_team.setVisibility(View.GONE);
                    iv_ranking.setVisibility(View.VISIBLE);
                    tv_ranking.setText("0" + (position));
                    tv_ranking.setTextColor(CommonUtil.getColor(R.color.white));
                    tv_fire_num.setTextColor(CommonUtil.getColor(R.color.color_FFED3F37));
                    GlideUtils.loadImage(context, CommonUtil.getDrawable(R.mipmap.ico_ranking2), iv_ranking);
                    break;
                case 3:
                    tv_my_team.setVisibility(View.GONE);
                    iv_ranking.setVisibility(View.VISIBLE);
                    tv_ranking.setText("0" + (position));
                    tv_ranking.setTextColor(CommonUtil.getColor(R.color.white));
                    tv_fire_num.setTextColor(CommonUtil.getColor(R.color.color_FFED3F37));
                    GlideUtils.loadImage(context, CommonUtil.getDrawable(R.mipmap.ico_ranking3), iv_ranking);
                    break;
                default:
                    tv_my_team.setVisibility(View.GONE);
                    ll_bg.setBackground(null);
                    if (helper.getAdapterPosition() <= 9) {
                        tv_ranking.setText("0" + (position));
                    } else {
                        tv_ranking.setText(position + "");
                    }
                    iv_ranking.setVisibility(View.GONE);
                    tv_fire_num.setTextColor(CommonUtil.getColor(R.color.color_333333));
                    tv_ranking.setTextColor(CommonUtil.getColor(R.color.color_FFDCDCDC));
                    break;
            }
        } else {
            switch (position) {
                case 0:
                    iv_ranking.setVisibility(View.VISIBLE);
                    tv_my_team.setVisibility(View.GONE);
                    tv_ranking.setText("0" + (position + 1));
                    tv_ranking.setTextColor(CommonUtil.getColor(R.color.white));
                    GlideUtils.loadImage(context, CommonUtil.getDrawable(R.mipmap.ico_ranking1), iv_ranking);
                    break;
                case 1:
                    iv_ranking.setVisibility(View.VISIBLE);
                    tv_my_team.setVisibility(View.GONE);
                    tv_ranking.setText("0" + (position + 1));
                    tv_ranking.setTextColor(CommonUtil.getColor(R.color.white));
                    GlideUtils.loadImage(context, CommonUtil.getDrawable(R.mipmap.ico_ranking2), iv_ranking);
                    break;
                case 2:
                    iv_ranking.setVisibility(View.VISIBLE);
                    tv_my_team.setVisibility(View.GONE);
                    tv_ranking.setText("0" + (position + 1));
                    tv_ranking.setTextColor(CommonUtil.getColor(R.color.white));
                    GlideUtils.loadImage(context, CommonUtil.getDrawable(R.mipmap.ico_ranking3), iv_ranking);
                    break;
                default:
                    ll_bg.setBackground(null);
                    tv_my_team.setVisibility(View.GONE);
                    tv_ranking.setText((position + 1) + "");
                    iv_ranking.setVisibility(View.GONE);
                    tv_ranking.setTextSize(20);
                    tv_ranking.setTextColor(CommonUtil.getColor(R.color.color_FFDCDCDC));
                    break;
            }
        }

//        rci_user_header.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
////                Intent intent = new Intent(context, MyHomeActivity.class);
////                intent.putExtra("userId", item.userId);
////                context.startActivity(intent);
//            }
//        });
    }
}
